<?php

  require_once "JooS/Helper/Abstract.php";

  require_once "JooS/Helper/Subject.php";

 /**
  * @package JooS
  * @subpackage PHPackager
  */
  
  class JooS_PHPackager_Helper_Call extends JooS_Helper_Abstract implements JooS_Helper_Subject {
    /**
     * @param string $name
     * @param array $args
     * @return mixed
     */
    public function __call($name, $args) {
      require_once "JooS/Loader.php";
      
      try {
        $helperClass = JooS_Loader::getClassName("PHPackager_Helper", $name, true);
        $helper = $this->helperBroker()->{$helperClass};
      }
      catch (JooS_Helper_Exception $e) {
        $helper = $name;
      }

      if (!is_callable($helper)) {
        require_once "JooS/PHPackager/Helper/Exception.php";

        throw new JooS_PHPackager_Helper_Exception("Function $name ($helperClass) was not found");
      }

      return call_user_func_array($helper, $args);
    }

    /**
     * @var JooS_Helper_Broker
     */
    private $_helperBroker = null;

    /**
     * @return JooS_Helper_Broker
     */
    public function helperBroker() {
      if ($this->_helperBroker === null) {
        require_once "JooS/Helper/Broker.php";

        $this->_helperBroker = JooS_Helper_Broker::newInstance($this->getSubject());
      }

      return $this->_helperBroker;
    }
  }
